﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Web;
using Blog.Models;
using BlogModel;

namespace Blog.Data
{
    public class PostRepository
    {
        public static List<PostModel> GetPostsByBlogId(int blogId)
        {
            var posts =
                from p in DB.Context.PostSet
                orderby p.CreationDate
                where p.BlogId == blogId && !p.IsDeleted
                select new PostModel
                {
                    PostId = p.PostId,
                    Title = p.Title,
                    Content = p.Content,
                    CreationDate = p.CreationDate,
                    FeaturedImage = p.FeaturedImage,
                    Comments = CommentRepository.GetCommentsByPostId(p.PostId)
                };

            return posts.ToList();
        }

        public static PostModel GetPostByPostId(int postId)
        {
            var post = DB.Context.PostSet
                     .FirstOrDefault(p => p.PostId == postId && !p.IsDeleted);

            if (post == null)
                return null;

            return new PostModel
            {
                PostId = post.PostId,
                Title = post.Title,
                Content = post.Content,
                CreationDate = post.CreationDate,
                FeaturedImage = post.FeaturedImage,
                Comments = CommentRepository.GetCommentsByPostId(post.PostId),
                UserId = post.Blog.UserId
            };
        }

        public static int InsertPost(PostModel post)
        {
            var newPost = new Post
            {
                Title = post.Title,
                Content = post.Content,
                CreationDate = post.CreationDate,
                FeaturedImage = post.FeaturedImage,
                BlogId = post.BlogId
            };
            DB.Context.PostSet.Add(newPost);
            DB.Context.SaveChanges();
            return newPost.PostId;
        }

        public static List<PostModel> GetAllPosts()
        {
            var posts = DB.Context.PostSet
                .OrderBy(p=>p.CreationDate)
                .Where(p => !p.IsDeleted)
                .Select(p=>new PostModel
                {
                    PostId = p.PostId,
                    Title = p.Title,
                    Content = p.Content,
                    CreationDate = p.CreationDate,
                    FeaturedImage = p.FeaturedImage                    
                });

            return posts.ToList();
        }

        public static int UpdatePost(PostModel post)
        {
            var newPost = new Post
            {
                PostId = post.PostId,
                Title = post.Title,
                Content = post.Content,
                CreationDate = post.CreationDate,
                FeaturedImage = post.FeaturedImage,
                BlogId = post.BlogId
            };
            DB.Context.PostSet.AddOrUpdate(p => p.PostId, newPost);
            return DB.Context.SaveChanges();
        }

        public static int DeletePost(int id)
        {
            var postToDelete = DB.Context.PostSet
                .FirstOrDefault(p => p.PostId == id);

            if (postToDelete != null)
                postToDelete.IsDeleted = true;

            return DB.Context.SaveChanges();
        }
        public static List<PostModel> GetPostsByUserId(int userId)
        {
            var blog = DB.Context.BlogSet
                .OrderBy(b=>b.CreationDate)
                .FirstOrDefault(b => b.UserId == userId && !b.IsDeleted);

            if (blog == null)
                return null;

            var posts =
                from p in blog.Post
                where !p.IsDeleted
                select new PostModel
                {
                    PostId = p.PostId,
                    Title = p.Title,
                    Content = p.Content,
                    CreationDate = p.CreationDate,
                    FeaturedImage = p.FeaturedImage 
                };

            return posts.ToList();
        }
    }
}
